package com.siecomzg.transport;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;

import com.siecomzg.proto.Message;
import com.siecomzg.tool.LogUtil;

import com.siecomzg.constconfig.ConstDefine;
import com.siecomzg.transport.bluetooth.BlueClient;
import com.siecomzg.transport.handler.ServiceHandler;
import com.siecomzg.transport.usb.USBClient;

import java.io.IOException;
import java.util.HashMap;

/**
 * Created on 2016/6/13.
 */
public class Distribution {

    private static final String TAG = "Distribution";
    /**
     * 根据命令类型分发
     */
    private HashMap<Integer, ServiceHandler> handlerMap = HandlerManger.getHandlerMap();

    public BlueClient mBlueClient = new BlueClient();
    public USBClient mUsbClient = USBClient.getInstance(null);
    public ConnectStatusCallback callback;


    public void init(String mac) throws IOException {
        BluetoothDevice device = BluetoothAdapter.getDefaultAdapter().getRemoteDevice(mac);
        init(device);
    }


    public void setConnectStatusCallback(ConnectStatusCallback callback) {

        this.callback = callback;
    }

    public void init(BluetoothDevice device) throws IOException {
        if (callback != null) {
            callback.onConnectStart();
        }
        try {
            mBlueClient.connect(device);
            ConstDefine.CONNECT_TYPE = ConstDefine.CONNECT_TYPE_BLUE; //蓝牙连接
        } catch (IOException e) {
            e.printStackTrace();

            if (callback != null) {
                callback.onDisConnect(e);
                CallbackMap.clearMap();
            }
            throw e;
        }
        if (callback != null) {
            callback.onConnected();
        }
        /**
         * 启动心跳包
         */

//        heartbeatHandler.startHeartbeat(ConstDefine.intervalMs);

    }

    public void startListen() {

        mBlueClient.listen(true);

        mBlueClient.setOnListen(new BlueClient.OnReceiveListen() {
            @Override
            public void onMessageReceive(Message.siecomproto msg,String message) {
                int module = msg.getModule();
                LogUtil.e(TAG, "onMessageReceive module id==" + module);
                if (handlerMap.containsKey(module)) {
                    ServiceHandler handler = handlerMap.get(module);
                    handler.onReceive(mBlueClient, msg);
                }
            }

            @Override
            public void onError(Throwable e) {
                LogUtil.e(TAG, "onError ==" + e.getMessage());
                if (callback != null) {
                    callback.onDisConnect(e);
                    CallbackMap.clearMap();
                }
            }
        });


    }
    public void startUSBListen(){
        LogUtil.e("USB module","setOnUSBReceiveListen");
        mUsbClient.setOnUSBReceiveListen(new USBClient.OnUSBReceiveListen() {
            @Override
            public void onUsbMsgReceive(Message.siecomproto msg) {
                LogUtil.e("USB module","进入回调 onUsbMsgReceive");
                int module = msg.getModule();
                LogUtil.e("USB module", msg.getMsgid()+"==" + module);
                if (handlerMap.containsKey(module)) {
                    ServiceHandler handler = handlerMap.get(module);
                    handler.onReceive(mUsbClient, msg);
                }
            }

            @Override
            public void onError(Throwable e) {
                LogUtil.e(TAG,"进入回调 startUSBListen onError " + e.getMessage());
            }
        });
    }

    /**
     * 断开连接
     */
    public void stop(){

        mBlueClient.setOnListen(null);
        mBlueClient.close();

    }

}
